<?php
namespace Admin\Controller;
use Think\Image;
use Think\Upload;

class ArticleController extends AuthController{
    protected $articleModel;

    public function _initialize()
    {
        $this->articleModel= new \Admin\Model\ArticleModel();
    }

    //内容列表
    public function index(){
        $this->page_title="يازما باشقۇرۇش";
        //$this->articles=M('article')->field('aid,title,author,add_time,category_cid')->select();
        $count= M('Article')->count();
        $Page= new \Think\Page($count,5);
        $show=$Page->show();
        $this->articles=$this->articleModel
                            ->join('blog_category ON blog_article.category_cid=blog_category.id')
                            ->field('aid,title,author,add_time,cname,blog_article.status')
                            ->limit($Page->firstRow.','.$Page->listRows)
                          ->select();
                            //->field('aid,title,author,add_time,category_cid')->select();
        //p($this->articles);die();
        $this->assign('Page',$show);
        $this->display();
    }

    //添加文章
     public function add(){
        $this->page_title="يازما قۇشۇش";
        //所有分类数据
        $this->categorys=D('Category')->getTree();
        // 所有标签数据
         $this->tags=D('Tag')->select();
        $this->display();
     }
     /*
      * 处理添加文件
      * */
     public function addHandler()
     {
          /*$rules=array(
              array('title','require','标题不能为空'),
              array('category_cid','require','请选择类'),
              array('description','require','简略内容不能为空'),
              array('content','require','内容不能为空'),
              array('author','require','作者名称不能为空'),
          );*/
          //$_POST['add_time']=time();
        $post=I('post.');
          //if(!M('article')->validate($rules)->create()) $this->error(M('article')->getError());
         if (!$this->articleModel->addArticle($post)) $this->error($this->articleModel->getError());
         //if (!M('article')->add()) $this->error('添加文章失败');
         $this->success('添加文章成功', U('index'),2);
     }
     /*
      * 上传图片
      * */
     public  function uploadThumb(){
         $config = array(
             'maxSize'    =>    3145728,
             'rootPath' =>      './Uploads/',
             'savePath'   =>    'Article/',
             'saveName'   =>    array('uniqid',''),
             'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
             'autoSub'    =>    true,
             'subName'    =>    array('date','Ymd'),);
         //实例化upload类
         $uploadify = new  Upload($config);// 实例化上传类
         $info=$uploadify->upload();
         if(!$info) $this->error($uploadify->getError());
         $thumb=$info['Filedata'];
         $savename=$thumb['savename'];
         $savepath=$thumb['savepath'];
         //形成完整的路径
         $fullpath='Uploads/'.$savepath.$savename;
         //p($fullpath);die();
         $newImage= new Image();
         //打开图片资源
         $newImage->open($fullpath);
         //裁剪
         $newImage->thumb(260,80,6)->save($fullpath);
         $this->ajaxReturn($fullpath);

     }
     /*
      * 编辑文章
      * */
     public function edit($aid){
         $this->page_title="يازما تەھرىرلەش";
         $this->articles=$this->articleModel->where(array('aid'=>$aid))->find();
         //p($this->articles);die();
         $this->tags=M('tag')->select();
          //p($this->tags);die();
         $this->category=D('category')->getTree();
         $this->article_tag=M('article_tag')->where(array('aid'=>$aid))->getField('tid',true);
         //p($this->article_tag);die();
         $this->display();
     }

     /*
      *  文章删除
      * */
     public  function delete(){
         $aid=I('post.aid');
         $thumb=$this->articleModel->where(array('aid'=>$aid))->getField('thumb');
         if(is_file($thumb) && file_exists($thumb)) unlink($thumb);
         if($this->articleModel->where(array('aid'=>$aid))->delete())
         {
             $data=['status'=>1,'message'=>'ئۈچۈرۈش غەلبىلىك بولدى'];
         }else{
             $data=['status'=>0,'message'=>'ئۈچۈرۈش مەغلۇپ بولدى،قايتا سىنىاڭ'];
         };
         $this->ajaxReturn($data);
     }
     /*
      * 处理编辑的数据*/
     public function saveHandler()
     {
         //p($_POST);
         $data=I('post.');
         //p($data);die();
         if(!$this->articleModel->save($data)) $this->error('编辑失败',M('Article')->getError());
         M('article_tag')->where(array('aid',$data['aid']))->delete();
         //M('article_tag')->save($data['aid']);
         if(isset($data['tag']) && is_array($data['tag']))
         {
             foreach ($data['tag'] as $tags){
                 $item=[
                     'aid'=>$data['aid'],
                     'tid'=>$tags,
                 ];
                 M('article_tag')->add($item);
             }
         }
         $this->success('编辑成功',U('Article/index'));
     }


}